import { createStore } from "vuex";
import { getinfo } from "../api/user";

// 创建一个新的 store 实例
const store = createStore({
  state() {
    return {
      //用户信息
      user: {},
    };
  },
  mutations: {
    //记录用户信息
    set_userinfo(state, user) {
      state.user = user;
    },
  },
  actions: {
    //获取当前登录用户信息
    getinfo({ commit }) {
      return new Promise((resolve, reject) => {
        getinfo()
          .then((res) => {
            commit("set_userinfo", res);
            resolve(res);
          })
          .catch((error) => reject(error));
      });
    },
  },
});

export default store;
